package com.tian;

import com.tian.mybatis.config.Configuration;
import com.tian.mybatis.config.XmlConfigBuilder;
import com.tian.mybatis.config.XmlMapperBuilder;
import com.tian.mybatis.io.Resources;
import com.tian.mybatis.session.SqlSession;
import com.tian.mybatis.session.SqlSessionFactory;
import com.tian.mybatis.session.SqlSessionFactoryBuilder;
import com.tian.test.User;
import com.tian.test.UserMapper;
import org.dom4j.DocumentException;

import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
//        String resource = "mapper/UserMapper.xml";
        //加载mybatis配置文件为字节流
        InputStream inputStream = Resources.getResourceAsStream(resource);
//                Configuration configuration = new Configuration();
//        XmlConfigBuilder xmlConfigBuilder=new XmlConfigBuilder(configuration);
//        xmlConfigBuilder.parse(inputStream);

//        XmlMapperBuilder xmlMapperBuilder = new XmlMapperBuilder(configuration);
//        xmlMapperBuilder.parse(inputStream);
//        System.out.println(configuration);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = (UserMapper) session.getMapper(UserMapper.class);
        User user1 = new User();
        user1.setId(2);
        user1.setUserName("tiange1111");
//        User user = userMapper.selectById1(user1);
//        User user = userMapper.selectById2(1,"tiange100");
//        User user = userMapper.selectById(1);
//        user1.setUserName("tiange100");
//        List<User> user = userMapper.selectByName(user1);
        int flag = userMapper.update(user1);
//        int flag = userMapper.insert(user1);
        System.out.println(flag == 1 ? "操作成功" : "操作失败");
//        List<User> user = userMapper.selectAll();
//        System.out.println(user);
//        System.out.println(inputStream);


//        Class<UserMapper> userMapperClass = UserMapper.class;
//        UserMapper userMapper = (UserMapper) Proxy.newProxyInstance(userMapperClass.getClassLoader(), new Class[]{userMapperClass}, new MyInvocationHandler());
//        User user = userMapper.selectById(1);
//        System.out.println(user);
    }
}

//class MyInvocationHandler implements InvocationHandler {
//
//    @Override
//    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        if (method.getName().equals("selectById")) {
//            User user = new User();
//            user.setId(1);
//            user.setUserName("tiange");
//            return user;
//        }
//        return null;
//    }
//}
